package cn.topevery.garden.dal.dao.impl;

import cn.topevery.garden.dal.dao.ChangeLogDao;
import cn.topevery.garden.entity.po.ChangeLog;
import cn.topevery.garden.entity.query.ChangeLogQuery;
import cn.topevery.garden.entity.vo.ChangeLogVO;
import cn.topevery.garden.framework.base.BaseDaoImpl;
import hw.topevery.framework.db.entity.DataPager;
import hw.topevery.framework.db.entity.SqlQueryMate;
import hw.topevery.framework.db.enums.ScriptConditionEnum;
import hw.topevery.framework.entity.DbCsp;
import hw.topevery.framework.entity.KeyValue;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Map;

/**
 * 
 *
 * @author code-generate
 * @date 2022-01-24
 */
@Repository
public class ChangeLogDaoImpl extends BaseDaoImpl<ChangeLog> implements ChangeLogDao {
    @Override
    public <P extends DbCsp> List<ChangeLog> getSearch(P para) {
        return null;
    }

    

    @Override
    public DataPager<ChangeLogVO> listPage(ChangeLogQuery query) {
        String table = "t_change_log tcl";

        String columns = "tcl.*";

        int pageIndex = query.pageIndex - 1;
        int pageSize = query.pageSize;

        SqlQueryMate sqlQueryMate = new SqlQueryMate(table).setColumns(columns).setOrderBy("tcl.c_id desc");
        sqlQueryMate
                .where(true, "tcl.c_db_status", ScriptConditionEnum.Eq, 0)
        ;

        return getDataPager(sqlQueryMate, pageSize, pageIndex, ChangeLogVO.class);
    }

    @Override
    public ChangeLog getEntityByColumn(String key,String value){
        KeyValue<String> map=new KeyValue<>();
        map.key=key;
        map.value=value;
        return getEntityByColumnEqValue(map);
    }

    public List<Map<String, Object>> getLogByObjId(Integer objId){
        return getMap("     SELECT\n" +
                "\tu.c_user_name AS user_name,\n" +
                "\tm.c_date,\n" +
                "\ts.c_name as change_name,\n" +
                "\ts.c_old_value,\n" +
                "\ts.c_new_value\n" +
                "FROM\n" +
                "\tt_change_log AS m\n" +
                "\tINNER JOIN t_change_detail AS s ON m.c_id = s.c_change_id\n" +
                "\tLEFT JOIN t_sys_user AS u ON m.c_user_id = u.c_id\n" +
                "WHERE\n" +
                "\tm.c_type = 1\n" +
                "\n" +
                "\tAND m.c_obj_id = '"+"' ORDER BY\n" +

                "\tm.c_date");
    }
}
